﻿using CL.Game.BLL;
using CL.Game.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.Admin.admin.report.finance
{
    public partial class rechargeyearreport : UI.AdminPage
    {
        protected string currentMonth = string.Empty;
        protected RechargeYearReportBLL bll = new RechargeYearReportBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected List<RechargeMonthReportEntity> GetDate()
        {
            currentMonth = txtStartTime.Text;
            if (string.IsNullOrEmpty(this.currentMonth))
            {
                this.currentMonth = DateTime.Now.ToString("yyyy");
                txtStartTime.Text = this.currentMonth;
            }

            string payType = ddlResult.Text;
            var listDate =  bll.QueryExtendDetails(currentMonth, payType);
            var listToji = bll.QueryExtendTJDetails(currentMonth, payType);
            RechargeMonthReportEntity zongJi = new RechargeMonthReportEntity();
            zongJi.Date = "总计";
            zongJi.PayType = "";
            for (int i = 0; i < listToji.Count; ++i)
            {
                zongJi.noPayAmount += listToji[i].noPayAmount;
                zongJi.successAmount += listToji[i].successAmount;
                zongJi.rateAmount += listToji[i].rateAmount;
            }
            listToji.Add(zongJi);
            listDate.AddRange(listToji);
            return listDate;
        }

        /// <summary>
        /// 数据导出到excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnbtnExport_Click(object sender, EventArgs e)
        {
            var list = GetDate();
            StringBuilder mystringbuilder = new StringBuilder();
            mystringbuilder.Append("<table border = '1' ><tr><td align = 'center' colspan=\"7\"> <b> 充值年度统计 </b></td></tr> ");
            mystringbuilder.Append("<tr><td align='center'><b>日期</b></td><td align='center'><b>合作商</b></td><td align='center'><b>支付方式</b></td><td align='center'><b>成功订单金额</b></td>");
            mystringbuilder.Append("<td align='center'><b>未支付金额</b></td><td align='center'><b>费率</b></td><td align='center'><b> 手续费</b></td>");
            mystringbuilder.Append("</tr>");
            if (list == null && list.Count <= 0)
            {
                return;
            }

            for (int i = 0; i < list.Count; i++)
            {
                mystringbuilder.Append("<tr>");
                mystringbuilder.Append("<td>" + list[i].Date + "</td>");
                mystringbuilder.Append("<td>" + bll.GetPartner(list[i].PayType) + "</td>");
                mystringbuilder.Append("<td>" + bll.GetPayMethod(list[i].PayType) + "</td>");
                mystringbuilder.Append("<td>" + Math.Round((double)list[i].successAmount / 100, 2) + "</td>");
                mystringbuilder.Append("<td>" + Math.Round((double)list[i].noPayAmount / 100, 2) + "</td>");
                mystringbuilder.Append("<td>" + Math.Round((double)list[i].rate / 100, 2) + "%</td>");
                mystringbuilder.Append("<td>" + Math.Round((double)list[i].rateAmount / 1000000, 2) + "</td>");
                mystringbuilder.Append("</tr>");
            }
            mystringbuilder.Append("</table>");

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("充值年度统计", Encoding.UTF8) + ".xls");
            Response.Write(mystringbuilder.ToString());
            Response.Flush();
            Response.End();

        }

        protected void BindData()
        {
            this.rptList.DataSource = GetDate();
            this.rptList.DataBind();
        }
    }
}